package com.yunbiao.ybbusstopplate.utils;

import android.os.Environment;
import android.util.Log;

import com.blankj.utilcode.util.FileUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

public class SSUtils {

    private static String GLOBAL_FOLDER = "/globalFolder";

    private static String ROOT = Environment.getExternalStorageDirectory().toString() + GLOBAL_FOLDER;

    private static String IS_MAIN_SCREEN = "isMainScreen";

    private static String IS_SUB_SCREEN = "isSubScreen";

    private static final String TAG = "SSUtils";

//    private static void d(String log) {
//        L.appD(TAG, log);
//    }

    // 是否接入usb mouse
    public static boolean isConnectUsbMouse() {
        boolean detectInputDeviceWithShell = detectInputDeviceWithShell("mouse");
        return detectInputDeviceWithShell;
    }

    public static boolean detectInputDeviceWithShell(String deviceName) {
        try {
            //获得外接USB输入设备的信息
            Process p = Runtime.getRuntime().exec("cat /proc/bus/input/devices");
            BufferedReader in = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = null;
            boolean detectInputDeviceWithShell = false;
            while ((line = in.readLine()) != null) {
                String deviceInfo = line.trim();
                //对获取的每行的设备信息进行过滤，获得自己想要的。
                if (deviceInfo.contains("mouse")) { // 包含鼠标
                    Log.d(TAG, "detectInputDeviceWithShell: " + deviceInfo);
                    detectInputDeviceWithShell = true;
                }

            }
            Log.d(TAG, "-----------------------");
            return detectInputDeviceWithShell;

        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean setIsMainScreen() {
        boolean createIsMainScreenFolder = createIsMainScreenFolder();

        boolean deleteIsSubScreenFolder = deleteIsSubScreenFolder();

        return createIsMainScreenFolder;
    }

    public static boolean setIsSubScreen() {
        boolean createIsSubScreenFolder = createIsSubScreenFolder();

        boolean deleteIsMainScreenFolder = deleteIsMainScreenFolder();

        return createIsSubScreenFolder;
    }

    public static void setNullSubScreen() {
        boolean deleteIsSubScreenFolder = deleteIsSubScreenFolder();

        boolean deleteIsMainScreenFolder = deleteIsMainScreenFolder();
    }

    public static boolean isMainScreen() {

        File isMainScreenFolderPath = new File(ROOT, IS_MAIN_SCREEN);
        boolean isMainScreenFolderPathExists = FileUtils.isDir(isMainScreenFolderPath);

        return isMainScreenFolderPathExists;
    }

    public static boolean isSubScreen() {
        File isSubScreenFolderPath = new File(ROOT, IS_SUB_SCREEN);
        boolean isSubScreenFolderPathExists = FileUtils.isDir(isSubScreenFolderPath);

        return isSubScreenFolderPathExists;
    }

    public static boolean isNullSubScreen() {
        File isSubScreenFolderPath = new File(ROOT, IS_SUB_SCREEN);
        boolean isSubScreenFolderPathExists = FileUtils.isDir(isSubScreenFolderPath);

        File isMainScreenFolderPath = new File(ROOT, IS_MAIN_SCREEN);
        boolean isMainScreenFolderPathExists = FileUtils.isDir(isMainScreenFolderPath);

        boolean isNullSubScreen = false;
        if(isSubScreenFolderPathExists&&isMainScreenFolderPathExists){
            isNullSubScreen = true;
        }

        return  isNullSubScreen;
    }

    private static boolean createIsMainScreenFolder() {
        //是否存在 创建文件夹
        boolean createIsMainScreen = FileUtils.createOrExistsDir(new File(ROOT, IS_MAIN_SCREEN));
        Log.d(TAG, "createIsMainScreen: " + createIsMainScreen);

        return createIsMainScreen;
    }

    private static boolean createIsSubScreenFolder() {
        //是否存在 创建文件夹
        boolean createIsSubScreen = FileUtils.createOrExistsDir(new File(ROOT, IS_SUB_SCREEN));
        Log.d(TAG, "createIsSubScreen: " + createIsSubScreen);

        return createIsSubScreen;
    }

    private static boolean deleteIsMainScreenFolder() {
        File dirToDelete = new File(ROOT, IS_MAIN_SCREEN);
        boolean isDelete = false;
        boolean dirToDeleteExists = dirToDelete.exists();
        if (dirToDeleteExists) {
            isDelete = dirToDelete.delete();
        }
        return isDelete;
    }

    private static boolean deleteIsSubScreenFolder() {
        File dirToDelete = new File(ROOT, IS_SUB_SCREEN);
        boolean isDelete = false;
        boolean dirToDeleteExists = dirToDelete.exists();
        if (dirToDeleteExists) {
            isDelete = dirToDelete.delete();
        }
        return isDelete;

    }
}
